package com.crm.crm.util;

import java.security.SecureRandom;

public class RandomStringGenerator {
    private static final String CHAR_LOWER = "abcdefghijklmnopqrstuvwxyz";
    private static final String CHAR_UPPER = CHAR_LOWER.toUpperCase();
    private static final String DIGIT = "0123456789";
    private static final String ALPHA_NUMERIC = CHAR_LOWER + CHAR_UPPER + DIGIT;

    private static SecureRandom random = new SecureRandom();

    /**
     * 生成指定长度的随机字符串
     */
    public static String generateRandomString(int length) {
        if (length < 1) throw new IllegalArgumentException("Length must be >= 1");
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            int rndCharIndex = random.nextInt(ALPHA_NUMERIC.length());
            sb.append(ALPHA_NUMERIC.charAt(rndCharIndex));
        }
        return sb.toString();
    }
}
